package exchange

import (
	"wscoin/exchange/binance"
	"wscoin/exchange/ftx"
	"wscoin/exchange/xt3Futures"
	"wscoin/exchange/xt3FuturesUser"
	"wscoin/exchange/xt4Spot"
	"wscoin/initialize"

	"github.com/gorilla/websocket"
)

// WS相关事件
type Eventer interface {
	OnEvent(string)
	OutEvent()
	PingMessageHandler(w *websocket.Conn) error
	GetURI() (uri string)
}

type EventFactory struct {
	exchange string
}

func NewEventFactory(exchange string) *EventFactory {
	return &EventFactory{
		exchange: exchange,
	}
}

// func (e *EventFactory) GetExchangeEvent() Eventer {
// 	switch e.exchange {
// 	case "binance":
// 		return binance.NewBinanceEvent()
// 	case "ftx":
// 		return ftx.NewFTXEvent()
// 	case "xt3Futures":
// 		return xt3Futures.NewXTContractEvent()
// 	case "xt3FuturesUser":
// 		return xt3FuturesUser.NewXTContractUserEvent()
// 	default:
// 		logs.Debug("[exchange] Not found exchange ...")
// 		panic("[exchange-GetExchangeEvent] Not found exchange")
// 	}
// }

/**
 * 1. 获取redis数据
 * 2. 建立MQ 连接
 * 3. 建立ws 连接
 * 4. 订阅WS 数据
 * 5. 接收WS 数据
 * 6  生产MQ 数据
 * 7  消费MQ 数据
 * 8  消费的数据写入Reids
**/

/**
 * 行情数据连接管理以及建立
 * 事件建立
 * MQ连接建立
 * WS连接建立
 * MQ连接关闭
 * WS连接关闭
 --------------
 * 关系
 * 一个市场可能存在多个MQ连接，或者多个WS连接
 * 观察者模式
 * 一个行情对应一个MQ的生产者和消费者 | 一个行情的订阅伴随着一个MQ连接的建立
 * 但是一个行情的建立不代表一个WS的连接建立
**/

type Server struct {
	start, end int
	exchange   string
}

func NewServer(exchange string, start, end int) *Server {
	return &Server{
		exchange: exchange,
		start:    start,
		end:      end,
	}
}

func (s *Server) Run(file string) {
	cfg := initialize.NewConfigure(file)
	cfg.Load()

	switch s.exchange {
	case "binance":
		// MQ建立
		engine := binance.NewEngine(s.start, s.end)
		engine.Start(cfg)

	case "ftx":
		engine := ftx.NewEngine(s.start, s.end)
		engine.Start(cfg)

	case "xt4Spot":
		engine := xt4Spot.NewEngine(s.start, s.end)
		engine.Start(cfg)

	case "xtContract":
		engine := xt3Futures.NewEngine(s.start, s.end)
		engine.Start(cfg)

	case "xtContractUser":
		engine := xt3FuturesUser.NewEngine(s.start, s.end)
		engine.Start(cfg)
	}
}
